package com.sx.micowell.framework.model

import androidx.lifecycle.MutableLiveData
import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.until.LogUtils
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.AppBanner
import com.miloyu.modelnet.response.HomeInfoVO
import com.sx.micowell.framework.adapter.binding.bean.FragmentTitleArgument
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction

/**
 * @Author: YY
 * @Time: 2021/9/7
 * @Describe TODO
 */
class Home2Model : BaseViewModel() {

    var type: Int = 1

    var homeDataEvent: SingleLiveEvent<HomeInfoVO> = SingleLiveEvent();

    // 页面
    var pageListData = MutableLiveListData<FragmentTitleArgument>()

    // 视频数据
    var homeVideoData: MutableLiveData<AppBanner> = MutableLiveData()

    init {
        initPage()
        reqHomeVideo()
    }

    // 刷新
    val onRefreshCommand: BindingCommand<Void> = BindingCommand(IBindingAction {
        LogUtils.d("onRefreshCommand")
        reqHome(type)
    })

    private fun initPage() {
        val arrayOf = arrayOf("全部", "上个月", "上周")
        val pageData = MutableLiveListData.Builder(pageListData)
        pageData.add(
            FragmentTitleArgument(
                arrayOf[0],
                RouteManager.getInstance().getHomeAllFragment()
            )
        )
        pageData.add(
            FragmentTitleArgument(
                arrayOf[1],
                RouteManager.getInstance().getHomeLastMonthFragment()
            )
        )
        pageData.add(
            FragmentTitleArgument(
                arrayOf[2],
                RouteManager.getInstance().getHomeLastWeekFragment()
            )
        )
        pageData.buildPost()
    }


    private fun reqHomeVideo() {
        addSubscribe(
            HttpClient.instance.apiService
                .getHomeVideo()
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<AppBanner>() {
                    override fun success(data: AppBanner?, msg: String?) {
                        homeVideoData.postValue(data)
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {
                        LogUtils.d("getHomeVideo $errorMsg")
                    }
                })
        )

    }

    fun reqHome(id: Int) {
        type = id
        addSubscribe(
            HttpClient.instance.apiService
                .reqHome(id)
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<HomeInfoVO?>() {
                    override fun success(data: HomeInfoVO?, msg: String?) {
                        LogUtils.d("reqHome ${data?.durationRows?.size}")
                        homeDataEvent.value = data
                        ui.refreshEvent.value = true
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {
                        LogUtils.d("reqHome $errorMsg")
                        ui.refreshEvent.value = false
                    }
                })

        )
    }
}